<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class Dorm extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 宿舍楼列表
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['name'])) {
            $where['name'] = $param['name'];
        }
        $list=model('dorm')->where('status',['>',-1])->where($where)->paginate(config('page_number'));
        $page = input('page')?:0;
        return $this->fetch('index', ['list' =>$list,'page'=>$page]);
    }
    /**
     * 后台添加宿舍楼
     */
    public function add(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['created_at'] = time();
            if($this->insert('dorm', $param,$rule = true, $field = true) === true){
                insert_admin_log('添加宿舍');
                $this->success('添加成功', url('admin/dorm/index'));
            }else{
                $this->error($this->errorMsg);
            }
        }
        $dormitory = model("dormitory")->where("status", [">", -1])->paginate(config(""));
        return $this->fetch('adddorm',['dormitory'=>$dormitory]);
    }
    /**
     * 编辑宿舍楼
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();

            $param['updated_at'] = time();
            if ($this->update('dorm', $param, input('_verify', true)) === true) {
                insert_admin_log('修改了宿舍楼');
                $this->success('修改成功', url('admin/dorm/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('adddorm', ['data' => model('dorm')->where('id', input('id'))->find()]);
    }
    /**
     * 删除宿舍楼
     */
    public function del()
    {
        if ($this->request->isGet()) {
            if ($this->delete('dorm', $this->request->param() ) === true) {
                insert_admin_log('删除了宿舍楼');
                $this->success('删除成功', url('admin/dorm/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
    }


}
